package com.woniuxy.meal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.meal.common.ConstantData;
import com.woniuxy.meal.common.RedisKey;
import com.woniuxy.meal.dao.StoreAttachmentMapper;
import com.woniuxy.meal.entity.StoreAttachment;
import com.woniuxy.meal.entity.StoreInfo;
import com.woniuxy.meal.dao.StoreInfoMapper;
import com.woniuxy.meal.exception.storeInfoException.StoreInfoException;
import com.woniuxy.meal.exception.storeInfoException.StoreInfoExceptionCode;
import com.woniuxy.meal.form.SearchStoreInfoForm;
import com.woniuxy.meal.model.Stores;
import com.woniuxy.meal.service.StoreInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.meal.service.dto.CategoryDto;
import com.woniuxy.meal.service.dto.StoreInfoDto;
import com.woniuxy.meal.service.dto.StoreInfoForEsDto;
import com.woniuxy.meal.service.dto.WoniuPageDto;
import com.woniuxy.meal.util.RedisUtil;
import io.swagger.annotations.ApiModelProperty;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zz
 * @since 2023年06月12日
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements StoreInfoService {

    @Autowired
    private ElasticsearchRestTemplate template;
    @Autowired
    private StoreAttachmentMapper storeAttachmentMapper;

    @Autowired
    private RedisUtil redisUtil;




    @Override
    public WoniuPageDto searchByEs(SearchStoreInfoForm form) {
        NativeSearchQueryBuilder nsqb = new NativeSearchQueryBuilder();
        if(form.getDesc()!=null){
            //搜索条件
            BoolQueryBuilder bqb = new BoolQueryBuilder();
            bqb.should().add(new MatchQueryBuilder("storeName",form.getDesc()));
            bqb.should().add(new MatchQueryBuilder("food.foodName",form.getDesc()));
            nsqb.withQuery(bqb);
//            //分页
//            Integer pageNum =form.getCurrent()==null?1:form.getCurrent();
//            Integer pageSize = form.getSize()==null?3:form.getSize();
//            nsqb.withPageable(PageRequest.of(pageNum-1,pageSize));
            //高亮
            HighlightBuilder hb = new HighlightBuilder();
            hb.preTags("<span style='color:red'>");
            hb.postTags("</span>");
            hb.field("storeName");
//        hb.fragmentSize(30);
//        hb.numOfFragments(1);
            nsqb.withHighlightBuilder(hb);
        }

        NativeSearchQuery nsq = nsqb.build();
        SearchHits<Stores> hits = template.search(nsq, Stores.class);
        List<StoreInfoForEsDto> dtos = new ArrayList<>();
        if(hits!=null && hits.getSearchHits().size()>0) {
            for (SearchHit<Stores> store : hits.getSearchHits()) {
                StoreInfoForEsDto storeInfoForEsDto = BeanUtil.toBean(store.getContent(), StoreInfoForEsDto.class);
                dtos.add(storeInfoForEsDto);
            }

        }
        WoniuPageDto<StoreInfoForEsDto> pageDto = new WoniuPageDto<>();
//        pageDto.setCurrent(form.getCurrent());
//        pageDto.setSize(form.getSize());
        pageDto.setTotal(Integer.parseInt(hits.getTotalHits()+""));
        pageDto.setList(dtos);
        return pageDto;
    }

    @Override
    public StoreInfoDto getOneStore(Long id) throws Exception {
        StoreInfo storeInfo = this.getById(id);
        if(storeInfo==null){
            throw new StoreInfoException(StoreInfoExceptionCode.STORE_IS_NOT_EXIST);
        }
        StoreInfoDto storeInfoDto = BeanUtil.toBean(storeInfo, StoreInfoDto.class);
        List<StoreAttachment> attachmentList = storeAttachmentMapper.selectList(new QueryWrapper<StoreAttachment>().eq("storeId", id));
        ArrayList<String> urls = new ArrayList<>();
        for (StoreAttachment storeAttachment : attachmentList) {
            urls.add(storeAttachment.getFilePath());
        }
//        redisUtil.zincrby(RedisKey.getStoreSaleKey(),id+"",0);
        double count = redisUtil.zscore(RedisKey.getStoreSaleKey(),id+"");
        System.out.println(count);
        storeInfoDto.setCount(count);
        storeInfoDto.setUrls(urls);
        return storeInfoDto;
    }

    @Override
    public StoreInfo getByAccountId(Long accountId) {
        return this.lambdaQuery().eq(StoreInfo::getAccountId, accountId).one();
    }

    @Override
    public List<CategoryDto> getCategories(Long storeId) {
        return baseMapper.listCategories(storeId);
    }



}
